<?php

namespace app\api\controller\ec;

use app\common\controller\Api;
use app\common\exception\UploadException;
use think\Config;
use think\Hook;
use think\Db;

/**
 * 公共接口
 */
class Eccompany extends Ecbase {

    public function _initialize() {
        parent::_initialize();
    }
    
    
    /*
     * 获取业务组信息
     */
    
    public function  getPubicPond(){
        $request_url = 'https://open.workec.com/v2/config/getPubicPond';
        $data = [];
        $response = $this->ecObj->doGet($request_url, $data);
        if($response['code'] == 200){
            $paichu_fields = ['id','createtime','content'];
            
            $this->mapFeildInsert($response['data'],$paichu_fields,'ec_publicpond','publicPondId');
        }
        $this->success('成功',$response);
    }
    
    
    /*
     * 获取来源信息
     */
    
    public function  getChannelSource(){
        $request_url = 'https://open.workec.com/v2/customer/getChannelSource';
        $data = [];
        $response = $this->ecObj->doGet($request_url, $data);       
        if($response['code'] == 200){
            $paichu_fields = ['sourceid','content'];
            
            $this->mapChannelInsert($response['data'],$paichu_fields,'ec_channel_source','id');
        }
        $this->success('成功',$response);
    }
    
    
    /*
     * 获取自定义字段
     */
    
    function  getFieldMapping(){
        $request_url = 'https://open.workec.com/v2/config/getFieldMapping';
        $data = [];
        $response = $this->ecObj->doGet($request_url, $data);
        
        if($response['code'] == 200){
            $paichu_fields = ['id','createtime','content'];
            
            $this->mapFeildInsert($response['data'],$paichu_fields,'ec_map_fields','fieldId');
        }
        $this->success('成功',$response);
    }
    
    /**
     * 获取标签信息
     */
    function  getLabelInfo(){
        $request_url = 'https://open.workec.com/v2/config/getLabelInfo';
        $data = [];
        $response = $this->ecObj->doGet($request_url, $data);
        
        if($response['code'] == 200){
            $paichu_fields = ['id','createtime','content'];
            $this->mapFeildInsert($response['data'],$paichu_fields,'ec_label','labelGroupId');
        }
        $this->success('成功',$response);
    }
    
    
    public function mapFeildInsert($data,$paichu_fields,$table,$exist_key){
        if(empty($data)) return false;
        
        $fields_list = Db::getFields('fa_'.$table);
        $fields = array_keys($fields_list);
        
        $end_fields = array_diff($fields,$paichu_fields);
        
        $end_data = [];
        foreach ($data as $value){
            
            $is_exist = db($table)->where($exist_key,$value[$exist_key])->count();
            if(!empty($is_exist)) continue;
            
            foreach ($end_fields as $v){
                if(is_array($value[$v])){
                    $middle_data[$v] = json_encode($value[$v],JSON_UNESCAPED_UNICODE) ;
                }else{
                    $middle_data[$v] = empty($value[$v]) ? '' : $value[$v];
                }
                
                
            }
            $middle_data['createtime'] = time();
            $middle_data['content'] = json_encode($value,JSON_UNESCAPED_UNICODE);
            
            
            $end_data[] = $middle_data;
            
        }
        
        if(empty($end_data)) return false;
        db($table)->insertAll($end_data);
    }
    
    
    public function mapChannelInsert($data,$paichu_fields,$table,$exist_key){
        if(empty($data)) return false;
        
        $fields_list = Db::getFields('fa_'.$table);
        $fields = array_keys($fields_list);
        
        $end_fields = array_diff($fields,$paichu_fields);
        
        $end_data = [];
        foreach ($data as $value){
            
            $is_exist = db($table)->where($exist_key,$value[$exist_key])->count();
            if(!empty($is_exist)) continue;
            
            foreach ($end_fields as $v){
                if(is_array($value[$v])){
                    $middle_data[$v] = json_encode($value[$v],JSON_UNESCAPED_UNICODE) ;
                }else{
                    $middle_data[$v] = empty($value[$v]) ? '' : $value[$v];
                }
                
                
            }            
            $middle_data['content'] = json_encode($value,JSON_UNESCAPED_UNICODE);
            
            
            $end_data[] = $middle_data;
            
        }
        
        if(empty($end_data)) return false;
        db($table)->insertAll($end_data);
    }
    
    
}
